package com.dongrj.framework.starter.sms;


import com.dongrj.framework.starter.sms.client.*;
import com.dongrj.framework.starter.sms.properties.*;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.WebClient;


@Configuration
@EnableConfigurationProperties(SmsProperties.class)
public class SmsClientAutoConfiguration {

    @Bean
    @ConditionalOnSmsActive(active = "aliyun")
    public SmsClient aliYunSmsClient(AliYunSmsProperties aLiYunSmsClient) {
        return new ALiYunSmsClient(aLiYunSmsClient);
    }

    @Bean
    @ConditionalOnProperty(value = "sms.active", havingValue = "feige")
    public SmsClient feiGeSmsClient(WebClient webClient, FeiGeSmsProperties feiGeSmsProperties) {
        return new FeiGeSmsClient(webClient, feiGeSmsProperties);
    }

    @Bean
    @ConditionalOnProperty(value = "sms.active", havingValue = "huawei")
    public SmsClient huaWeiYunSmsClient(HuaWeiYunSmsProperties huaWeiYunSmsProperties) {
        return new HuaWeiYunSmsClient(huaWeiYunSmsProperties);
    }

    @Bean
    @ConditionalOnProperty(value = "sms.active", havingValue = "tencent")
    public SmsClient tencentYunSmsClient(TencentYunSmsProperties tencentYunSmsProperties) {
        return new TencentYunSmsClient(tencentYunSmsProperties);
    }

    @Bean
    @ConditionalOnProperty(value = "sms.active", havingValue = "weidao")
    public SmsClient weiDaoYunSmsClient(WebClient webClient, WeiDaoYunSmsProperties weiDaoYunSmsProperties) {
        return new WeiDaoYunSmsClient(webClient, weiDaoYunSmsProperties);
    }

    @Bean
    @ConditionalOnMissingBean(WebClient.class)
    public WebClient webClient() {
        return WebClient
                .builder()
                .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                .build();
    }
}
